R and RStudio

Jeremy Springman

University of Pennsylvania

Global Development: Intermediate Topics in Politics, Policy, and Data

PSCI 3200 - Spring 2026

R and RStudio

Resources

What are R and RStudio?

  • In the quantitative methods tutorial, we will use R and RStudio to illustrate basic statistical concepts
  • R is a programming language and free computing environment for statistical computing and graphics
  • RStudio is an integrated development environment (IDE) that provides an interface by adding many convenient features and tools
  • Think of R as a car’s engine and RStudio as the dashboard

Installing R

  • Download and install R by going to https://cloud.r-project.org/
    • Windows: Click on “Download R for Windows”, then click on “base”, then click on the Download link
    • macOS user: Click on “Download R for macOS”, then under “Latest release:” click on R-X.X.X.pkg, where R-X.X.X is the version number
    • Linux user: Click on “Download R for Linux” and choose your distribution for more information on installing R for your setup

Installing RStudio

  • Download and install RStudio at https://www.rstudio.com/products/rstudio/download/
    • Scroll down to “Installers for Supported Platforms” near the bottom of the page
    • Click on the download link corresponding to your computer’s operating system

Using R via RStudio

  • Using R by opening RStudio

Using R via RStudio

  • After you open RStudio, you should see something like this

Peronalization

  • Customize the appearance of your R IDE by going to Tools -> Global Options -> Appearance

Layout

  • Rstudio contains 4 panes: source, console, environment, and viewer
  • The left pane is the source where you write code
  • Try typing 2 + 2 into the source panel and hitting ENTER
    • The console panel pop-up in the bottom-left and return a value of 4

[INSERT SCREEN RECORDING]

Layout

  • The top-right includes the Environment panel
    • This contains information about any objects that you load into your environment
    • Type sum = 2 + 2 into the source and hit ENTER
    • The object sum should appear in your environment
  • The bottom-right includes the Viewer panel, where you will be able to see graphics that you generate

Basic programming concepts

  • Running code: telling R to perform an act by giving it commands in the source or console
  • Objects: where values are saved in R. On the last slide, you created the object sum.
  • Data types:
    • Integers are whole numbers like -1, 0, 2, 4092
    • Doubles are integers with decimal values like -24.932 and 0.8
    • Logicals are either TRUE or FALSE
    • Characters are text or strings like "hello world" and "welcome to R"

Basic programming concepts

  • Vectors are a series of values. These are created using the c() function. For example, c(6, 11, 13, 31) creates a four element vector of integers.
  • Factors are a group of characters/strings with a fixed number of unique values
  • Data frames are objects where the rows correspond to observations and the columns correspond to variables that describe the observations

Basic programming concepts

  • Conditionals:
    • Testing for equality in R is done using ==. For example, 2 + 1 == 3 will return TRUE
    • Boolean algebra: Operators such as < (less than), <= (less than or equal), and !=(not equal to). For example, 3 + 5 <= 1 will return FALSE
    • Logical operators: & represent “and” while | represents “or.” For example, (2 + 1 == 3) & (2 + 1 == 4) returns FALSE since both clauses are not TRUE

Loading Packages

  • R gives you accesss to thousands of “packages” that are created by users
  • Packages contain datasets and bundles of code called “functions” that can execute specific tasks
  • Use install.packages() to install a package
    • Insert the name of the package contained in quotation marks
    • Start by installing the dplyr package

Loading Data

  • Load data into your environment by “reading-in” a spreadsheet
  • Spreadsheets should be saved as a .csv file
  • Use read.csv() to pull data from a spreadsheet on your harddrive into your R/RStudio environment
    • Within the parentheses, add the full file pathway where the .csv file is stored

[INSERT SCREEN RECORDING]

Errors, warnings, and messages

  • When you run code, R often provides feedback in the console
  • There are 3 main types of feedback that appear in RED
    • Errors: When the RED text is prefaced with “Error in…”, your code will not work. The error message will try to explain the problem.
    • Warnings: When the red text is prefaced with “Warning:”, your code will still work, but there still might be a problem. The warning message will try to explain the problem.
    • Messages: When the red text doesn’t start with “Error” or “Warning”, it’s just a friendly message